/**
 * Created by TomSean on 2017/11/29.
 */
var Recharge = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}
Recharge.prototype = {
    init : function () {
        this.$parent.find('#ownerName').html(ownerManager.entity.ownerName);
        this.$parent.find('#balance').html(ownerManager.entity.balance);
        if (isEmpty(ownerManager.entity.deposit)||ownerManager.entity.deposit==0) {
            this.$parent.find('#deposit').html(0);
        } else {
            this.$parent.find('#deposit').val(ownerManager.entity.deposit);
            this.$parent.find('#deposit').attr('disabled', 'disabled');
            this.$parent.find('#deposit').attr('readonly', 'readonly');
        }
        this.$parent.find('.rent-title small').html(ownerManager.entity.organization.organizationName);
        this.entity = ownerManager.entity;
        this.loadRechargeLog();
    },
    loadRechargeLog : function () {
        var $this = this;
        JsonpAjax(baseApiPath + 'owner_info/recharge/list/'+this.entity.ownerCode, '', 'json', function (res){
            if (isEmpty(res) || res.length == 0) {
                return;
            }
            var tbody = $this.$parent.find('.rent-content tbody');
            tbody.html('');
            for (var i = 0; i < res.length; i++) {
                var tr = $('<tr></tr>');
                tr.append('<td>'+res[i].chargeTime+'</td>');
                tr.append('<td>'+res[i].chargeMoney+'</td>');
                tbody.append(tr);
            }
        }, true);
    },
    recharge : function () {
        var deposit = this.$parent.find('#deposit').val();
        var money = this.$parent.find('#money').val();
        var $this = this;
        try {
            money = parseFloat(money);
            deposit = parseFloat(deposit);
            if (money <= 0) {
                notification({'title':'错误', 'message':'充值金额不能小于0！'},'error');
                return;
            }
            if (deposit <= 0) {
                notification({'title':'错误', 'message':'押金不能小于0！'},'error');
                return;
            }
        } catch (e) {
            notification({'title':'错误', 'message':'请输入正确的金额'},'error');
            return;
        }
        notification({'title':'提示', 'message':'是否确认充值'},'confirm', {
            callback : function () {
                var dataT = {'deposit' : deposit};
                JsonpAjax(baseApiPath + 'owner_info/recharge/' + $this.entity.ownerCode + '/' + money, JSON.stringify(dataT), 'json', function(res){
                    $this.$parent.find('#balance').html(res);
                    closeWindow();
                }, false);
            }
        });
    }
}